﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Text;

namespace Jy123.Web.home.MiniContent
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Album : IHttpHandler
    {
        private int userID;
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            
            StringBuilder sbHtml = new StringBuilder(context.Cache["user_minicontent"].ToString());//sbContent---------大stringbuilder

            string strUserID = context.Request.QueryString["MiniContentUserID"];
            string strAlbumUl = string.Empty;
            if (!string.IsNullOrEmpty(strUserID))
            {
                strUserID = strUserID.Substring(0,strUserID.IndexOf("?"));
                strAlbumUl = strAlbumUlBeAccess(context, Convert.ToInt32(strUserID));
            }
            else
            {
                strAlbumUl = strAlbumUlMy(context, userID);
            }
            if (strAlbumUl.Trim() == "")
            {
                strAlbumUl = "暂无动态!";
            }
            sbHtml = sbHtml.Replace("$ContentMini_ul$", strAlbumUl.ToString());
            context.Response.ContentType = "text/plain";
            context.Response.Write(sbHtml.ToString());
        }
        /// <summary>
        /// 我的mini相册列表(查我的好友的)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userID">当前用户的ID</param>
        /// <returns>string</returns>
        private string strAlbumUlMy(HttpContext context,int userID)
        {
            List<int?> count;
            //用户列表
            Jy123.BLL.User.Friend bllUserFrd = new Jy123.BLL.User.Friend();
            StringBuilder sbUlFriend = new StringBuilder();//好友列表            
            string strUlFriend = context.Cache["user_minicontent_ul"].ToString();//ul-----------------2
            //string strAlbumList = context.Cache["user_minicontent_ul_albumlist"].ToString();//li
            DataTable dtUserFrdList = bllUserFrd.GetFriendList(1, 10, userID, out count).Tables[0];
            for (int k = 0; k < dtUserFrdList.Rows.Count; k++)
            {
                string strUlFrd = strUlFriend;
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtUserFrdList.Rows[k]["FFriend"]));

                strUlFrd = strUlFrd.Replace("$UserID$", dtUserFrdList.Rows[k]["FFriend"].ToString())
                .Replace("$UserName$", dtUserFrdList.Rows[k]["UserName"].ToString())
                .Replace("$Portrait$", new AppCode.CreateDir().ReDraw(45, 45, userPortrait));
                #region 相册列表
                //List<int?> count;
                StringBuilder sbAlbumList = new StringBuilder();//相册列表----------------3
                DataTable dtAlbumList = new BLL.User.Album().AlbumList_MiniContent(1, 10, "FUser = '" + dtUserFrdList.Rows[k]["FFriend"] + "'and isCover=1", "CreateTime DESC", out count).Tables[0];
                string strAlbum_a = context.Cache["user_minicontent_ul_albumlist_a"].ToString();//----------------4
                for (int i = 0; i < dtAlbumList.Rows.Count; i++)
                {
                    string strAlbumRow = strAlbum_a;
                    strAlbumRow = strAlbumRow.Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());
                    strAlbumRow = strAlbumRow.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(45, 0, new BLL.User.Album().AlbumCover((int)dtAlbumList.Rows[i]["ID"])))
                    .Replace("$Title$", dtAlbumList.Rows[i]["Title"].ToString());

                    sbAlbumList.Append(strAlbumRow);
                }
                #endregion
                //strAlbumList = strAlbumList.Replace("$ul_AlbumList_a$", sbAlbumList.ToString());
                strUlFrd = strUlFrd.Replace("$ul_AlbumList_a$", sbAlbumList.ToString());
                strUlFrd = strUlFrd.Replace("$AlbumCount$", new BLL.User.Album().AlbumCount(dtUserFrdList.Rows[k]["FFriend"].ToString()).ToString());
                if (string.IsNullOrEmpty(sbAlbumList.ToString().Trim()))
                    continue;
                sbUlFriend.Append(strUlFrd);
            }
            return sbUlFriend.ToString();
        }
        /// <summary>
        /// 被访问者的mini相册列表(被访问者的相册列表)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userID">被访问者的ID</param>
        /// <returns>string</returns>
        private string strAlbumUlBeAccess(HttpContext context, int userID)
        {
            List<int?> count;
            StringBuilder sbAlbumList = new StringBuilder();//相册列表----------------3
            DataTable dtAlbumList = new BLL.User.Album().AlbumList_MiniContent(1, int.MaxValue, "  (FUser = '"+userID+"') AND(IsCover = 1)", "createTime desc", out count).Tables[0];
            string strAlbum_a = context.Cache["user_minicontent_ul_albumlist_their"].ToString();//----------------4
            for (int i = 0; i < dtAlbumList.Rows.Count; i++)
            {
                string strAlbumRow = strAlbum_a;
                //strAlbumRow = strAlbumRow.Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());
                strAlbumRow = strAlbumRow.Replace("$AlbumCover$", new AppCode.CreateDir().ReDraw(45, 0, new BLL.User.Album().AlbumCover((int)dtAlbumList.Rows[i]["ID"])))
                .Replace("$Title$", dtAlbumList.Rows[i]["Title"].ToString());

                    //相册中的4张相片列表
                    StringBuilder sbPhotoList = new StringBuilder();
                    string strPhoto = context.Cache["user_minicontent_ul_albumlist_their_a"].ToString();
                    DataTable dtPhotoList = new BLL.User.Album().PhotoList((int)dtAlbumList.Rows[i]["ID"], "PostTime DESC", 1, 4).Tables[0];
                    for (int j = 0; j < dtPhotoList.Rows.Count; j++)
                    {
                        string strP = strPhoto;
                        strP = strP.Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());
                        strP = strP.Replace("$PhotoID$",dtPhotoList.Rows[j]["ID"].ToString());
                        strP = strP.Replace("$Title$", dtPhotoList.Rows[j]["Title"].ToString());
                        strP = strP.Replace("$FilePath$",new AppCode.CreateDir().ReDraw(45,0,dtPhotoList.Rows[j]["FilePath"].ToString()));
                        sbPhotoList.Append(strP);
                    }
                strAlbumRow = strAlbumRow.Replace("$AlbumID$", dtAlbumList.Rows[i]["ID"].ToString());
                strAlbumRow = strAlbumRow.Replace("$PhotoCount$", new BLL.User.Album().PhotoCount((int)dtAlbumList.Rows[i]["ID"]).ToString());
                strAlbumRow = strAlbumRow.Replace("$ul_PhotoList_a$", sbPhotoList.ToString());
                    sbAlbumList.Append(strAlbumRow);
            }
            return sbAlbumList.ToString();

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
